In [1]:
import os, geopandas as gpd
import matplotlib.pyplot as plt

departments=gpd.read_file(os.path.join("maps","Departamentos","departamentos_geo.shp"))
rivers=gpd.read_file(os.path.join("maps","Rios","Rios.shp"))
cities=gpd.read_file(os.path.join("maps","Ciudades","CapitalDepartamental.shp"))
In [2]:
type(departments)
Out[2]:
geopandas.geodataframe.GeoDataFrame
In [3]:
type(rivers)
Out[3]:
geopandas.geodataframe.GeoDataFrame
In [4]:
type(cities)
Out[4]:
geopandas.geodataframe.GeoDataFrame
In [5]:
departments.shape
Out[5]:
(9, 4)
In [6]:
rivers.shape
Out[6]:
(940, 5)
In [7]:
cities.shape
Out[7]:
(9, 4)
In [8]:
departments.head()
Out[8]:
id c_ut DEPARTAMEN geometry
0 0 03 Cochabamba POLYGON ((-65.79998 -18.00967, -65.80067 -18.0...
1 1 05 Potosí POLYGON ((-66.68310 -18.48254, -66.68310 -18.4...
2 2 06 Tarija POLYGON ((-65.05251 -22.08335, -65.13966 -22.0...
3 3 07 Santa Cruz POLYGON ((-57.63652 -17.98160, -57.61941 -18.0...
4 4 09 Pando POLYGON ((-69.00170 -11.00922, -69.00106 -11.0...
In [9]:
rivers.head()
Out[9]:
gml_id ID NOMBRE RIOS_LIM geometry
0 Rios.1 0 Río Kharamanu NaN LINESTRING (-67.70626 -10.69996, -67.70825 -10...
1 Rios.2 0 Río Paucerna NaN LINESTRING (-60.81141 -14.27497, -60.80161 -14...
2 Rios.3 0 Río Supaymanu NaN LINESTRING (-68.56330 -11.46430, -68.56529 -11...
3 Rios.4 0 NaN NaN LINESTRING (-68.92574 -11.17870, -68.92760 -11...
4 Rios.5 0 NaN NaN LINESTRING (-66.50216 -10.69610, -66.50555 -10...
In [10]:
cities.head()
Out[10]:
gml_id ID NOMBRE geometry
0 CapitalDepartamental.1 0 COBIJA POINT (-68.77689 -11.03397)
1 CapitalDepartamental.2 0 TRINIDAD POINT (-64.92070 -14.84987)
2 CapitalDepartamental.3 0 LA PAZ POINT (-68.13798 -16.49165)
3 CapitalDepartamental.4 0 COCHABAMBA POINT (-66.17044 -17.38918)
4 CapitalDepartamental.5 0 ORURO POINT (-67.11293 -17.96623)
In [11]:
departments.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 9 entries, 0 to 8
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   id          9 non-null      int64   
 1   c_ut        9 non-null      object  
 2   DEPARTAMEN  9 non-null      object  
 3   geometry    9 non-null      geometry
dtypes: geometry(1), int64(1), object(2)
memory usage: 416.0+ bytes
In [12]:
rivers.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 940 entries, 0 to 939
Data columns (total 5 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   gml_id    940 non-null    object  
 1   ID        940 non-null    int64   
 2   NOMBRE    189 non-null    object  
 3   RIOS_LIM  13 non-null     object  
 4   geometry  940 non-null    geometry
dtypes: geometry(1), int64(1), object(3)
memory usage: 36.8+ KB
In [13]:
cities.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 9 entries, 0 to 8
Data columns (total 4 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   gml_id    9 non-null      object  
 1   ID        9 non-null      int64   
 2   NOMBRE    9 non-null      object  
 3   geometry  9 non-null      geometry
dtypes: geometry(1), int64(1), object(2)
memory usage: 416.0+ bytes
In [14]:
departments.plot()
rivers.plot()
cities.plot()
Out[14]:
<AxesSubplot:>
In [15]:
departments.crs
Out[15]:
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...>
Name: WGS 84
Axis Info [ellipsoidal]:
- lon[east]: Longitude (Degree)
- lat[north]: Latitude (Degree)
Area of Use:
- undefined
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [16]:
rivers.crs
Out[16]:
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...>
Name: WGS 84
Axis Info [ellipsoidal]:
- lon[east]: Longitude (Degree)
- lat[north]: Latitude (Degree)
Area of Use:
- undefined
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [17]:
cities.crs
Out[17]:
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...>
Name: WGS 84
Axis Info [ellipsoidal]:
- lon[east]: Longitude (Degree)
- lat[north]: Latitude (Degree)
Area of Use:
- undefined
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [18]:
base = departments.plot(facecolor="white", edgecolor='black', linewidth=0.5,figsize=(12,12))
cities.plot(marker='D', color='red', markersize=15,alpha=1,
            ax=base) 
rivers.plot(edgecolor='blue', linewidth=0.35,
            ax=base)
Out[18]:
<AxesSubplot:>
In [19]:
crs = 'EPSG:4326'
departments.crs = crs
rivers.crs = crs
cities.crs = crs
In [20]:
import folium


m = cities.explore(color="red", 
                   name="cities")#optional

m = rivers.explore(m=m, color="blue",
                   name="rivers")#optional
#folium.LayerControl().add_to(m) #optional
m
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [21]:
la_paz=departments[departments.DEPARTAMEN=='La Paz']
In [22]:
cities_clipped = gpd.clip(gdf=cities,
                          mask=la_paz)
rivers_clipped = gpd.clip(gdf=rivers,
                               mask=la_paz)
In [23]:
base = la_paz.plot(facecolor="greenyellow", edgecolor='black', linewidth=0.4,figsize=(5,5))
cities_clipped.plot(marker='+', color='red', markersize=15,ax=base)
rivers_clipped.plot(edgecolor='blue', linewidth=0.5,ax=base)
Out[23]:
<AxesSubplot:>
In [24]:
la_pazCoord=[-16.5, -68.15]
In [25]:
m = cities.explore(location=la_pazCoord,
                   zoom_start=10,
                   tiles='CartoDB positron',
                   color='red',
                   name="cities") #optional
m = rivers.explore(m=m, color="blue",
                   name="rivers")#optional
# folium.LayerControl().add_to(m) #optional
m
Out[25]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [26]:
base = la_paz.plot(facecolor="lightgreen", edgecolor='black', linewidth=0.4,figsize=(5,5))
map2=cities_clipped.plot(marker='D', color='red', markersize=15,ax=base)
mapEnd=rivers_clipped.plot(edgecolor='blue', linewidth=0.5,ax=base)
plt.savefig(os.path.join("maps",'mapEnd.jpg'))
In [27]:
departments.to_file(os.path.join("maps","worldMaps.gpkg"), layer='departments', driver="GPKG")
cities.to_file(os.path.join("maps","worldMaps.gpkg"), layer='cities', driver="GPKG")
rivers.to_file(os.path.join("maps","worldMaps.gpkg"), layer='rivers', driver="GPKG")
In [28]:
worldMaps='https://github.com/FabianRomero20/Examen1.2/raw/main/maps/worldMaps.gpkg'
In [29]:
from  fiona import listlayers

listlayers(worldMaps)
Out[29]:
['departments', 'cities', 'rivers']
In [30]:
departments=gpd.read_file(worldMaps,layer='departments')
cities=gpd.read_file(worldMaps,layer='cities')
rivers=gpd.read_file(worldMaps,layer='rivers')
In [31]:
base = departments.plot(facecolor='gainsboro')
cities.plot(ax=base, markersize=0.5, color='red') 
rivers.plot(ax=base, linewidth=0.5)
Out[31]:
<AxesSubplot:>
In [ ]: